# AFAC2025 基金预测项目 Makefile
# 项目管理和数据获取自动化

.PHONY: help setup update-data clean train test install-deps check-data

# 默认目标
help:
	@echo "AFAC2025 基金预测项目 - 可用命令:"
	@echo ""
	@echo "数据管理:"
	@echo "  setup           - 初始化项目和数据子模块"
	@echo "  update-data     - 更新训练数据到最新版本"
	@echo "  check-data      - 检查数据文件状态"
	@echo "  sync-latest     - 同步最新的fund_apply_redeem_series.csv"
	@echo ""
	@echo "环境管理:"
	@echo "  install-deps    - 安装Python依赖"
	@echo "  clean           - 清理临时文件和缓存"
	@echo ""
	@echo "训练和测试:"
	@echo "  train           - 运行RNN模型训练"
	@echo "  train-enhanced  - 运行增强特征训练"
	@echo "  test            - 运行所有测试"
	@echo "  baseline        - 运行基线模型"
	@echo ""
	@echo "特征处理:"
	@echo "  process-features - 处理和缓存特征"
	@echo "  cache-market    - 缓存市场特征"
	@echo "  cache-llm       - 缓存LLM特征"

# 项目初始化
setup:
	@echo "🚀 初始化AFAC2025项目..."
	@if [ ! -d "data" ]; then \
		echo "📥 添加数据子模块..."; \
		git submodule add https://github.com/AFAC-2025/AFAC2025_train_data.git data || \
		echo "⚠️  子模块添加失败，请手动克隆数据仓库"; \
	fi
	@$(MAKE) update-data
	@$(MAKE) install-deps
	@echo "✅ 项目初始化完成!"

# 更新数据子模块
update-data:
	@echo "📊 更新训练数据..."
	@if [ -d "data" ]; then \
		cd data && git pull origin main; \
	else \
		echo "⚠️  数据目录不存在，尝试克隆..."; \
		git clone https://github.com/AFAC-2025/AFAC2025_train_data.git data || \
		echo "❌ 无法克隆数据仓库，请检查网络连接"; \
	fi
	@$(MAKE) sync-latest

# 同步最新的fund_apply_redeem_series.csv
sync-latest:
	@echo "🔄 同步最新数据文件..."
	@if [ -d "data" ]; then \
		LATEST_DIR=$$(ls -1 data/ | grep -E '^[0-9]{8}_update$$' | sort -r | head -1); \
		if [ -n "$$LATEST_DIR" ] && [ -f "data/$$LATEST_DIR/fund_apply_redeem_series.csv" ]; then \
			echo "📋 发现最新数据: $$LATEST_DIR"; \
			cp "data/$$LATEST_DIR/fund_apply_redeem_series.csv" ./fund_apply_redeem_series.csv; \
			echo "✅ 已同步最新的fund_apply_redeem_series.csv"; \
		else \
			echo "⚠️  未找到最新数据文件"; \
		fi; \
	else \
		echo "❌ 数据目录不存在"; \
	fi

# 检查数据状态
check-data:
	@echo "🔍 检查数据文件状态..."
	@if [ -d "data" ]; then \
		echo "📁 数据子模块状态:"; \
		cd data && git log --oneline -5; \
		echo ""; \
		echo "📋 可用数据版本:"; \
		ls -la data/ | grep -E '^d.*[0-9]{8}_update$$' || echo "无可用数据版本"; \
		echo ""; \
	fi
	@if [ -f "fund_apply_redeem_series.csv" ]; then \
		echo "✅ 本地fund_apply_redeem_series.csv存在"; \
		echo "📊 文件大小: $$(du -h fund_apply_redeem_series.csv | cut -f1)"; \
		echo "📅 修改时间: $$(stat -f "%Sm" fund_apply_redeem_series.csv)"; \
	else \
		echo "❌ 本地fund_apply_redeem_series.csv不存在"; \
	fi
	@if [ -f "fund_enhanced_features.csv" ]; then \
		echo "✅ fund_enhanced_features.csv存在"; \
		echo "📊 文件大小: $$(du -h fund_enhanced_features.csv | cut -f1)"; \
	else \
		echo "❌ fund_enhanced_features.csv不存在"; \
	fi

# 安装Python依赖
install-deps:
	@echo "📦 安装Python依赖..."
	@if [ -f "requirements.txt" ]; then \
		pip install -r requirements.txt; \
	else \
		pip install pandas numpy torch scikit-learn matplotlib seaborn jupyter; \
	fi || echo "⚠️  部分依赖安装失败，请手动安装"

# 清理临时文件
clean:
	@echo "🧹 清理临时文件..."
	@rm -rf __pycache__/
	@rm -rf .pytest_cache/
	@rm -f *.pyc
	@rm -f .DS_Store
	@echo "✅ 清理完成"

# 训练模型
train:
	@echo "🧠 开始RNN模型训练..."
	@python original_test_rnn.py

# 增强特征训练
train-enhanced:
	@echo "🚀 开始增强特征RNN训练..."
	@python test_rnn.py

# 基线模型
baseline:
	@echo "📊 运行基线模型..."
	@python baseline.py

# 处理特征
process-features:
	@echo "⚙️ 处理和生成特征..."
	@python process_features_main.py

# 快速特征处理
quick-features:
	@echo "⚡ 快速特征处理..."
	@python quick_process_features.py

# 缓存市场特征
cache-market:
	@echo "📈 缓存市场特征..."
	@python market_cache_example.py

# 缓存LLM特征
cache-llm:
	@echo "🤖 缓存LLM特征..."
	@python test_llm_fallback_strategy.py

# 运行所有测试
test:
	@echo "🧪 运行测试套件..."
	@python test_feature_cache_system.py
	@python test_market_cache.py
	@python test_enhanced_features.py
	@python test_all_funds.py

# 特定测试
test-rnn:
	@echo "🧠 测试RNN模型..."
	@python test_rnn.py

test-features:
	@echo "🔧 测试特征处理..."
	@python test_enhanced_features.py

test-cache:
	@echo "💾 测试缓存系统..."
	@python test_feature_cache_system.py

# 数据分析
analyze:
	@echo "📊 数据分析..."
	@jupyter notebook test_rnn.ipynb

# 生成预测结果
predict:
	@echo "🎯 生成预测结果..."
	@python test_enhanced_prediction_features.py

# 检查项目状态
status:
	@echo "📋 项目状态检查..."
	@echo "Git状态:"
	@git status --short
	@echo ""
	@$(MAKE) check-data
	@echo ""
	@echo "Python环境:"
	@python --version
	@echo ""
	@echo "关键文件状态:"
	@ls -la *.py | head -5
	@ls -la *.csv | head -3

# 完整工作流
workflow: setup process-features train test
	@echo "🎉 完整工作流执行完成!"

# 开发模式 - 监控文件变化并重新训练
dev:
	@echo "👨‍💻 开发模式 - 监控文件变化..."
	@echo "修改Python文件后将自动重新训练"
	@while true; do \
		inotifywait -e modify *.py 2>/dev/null && \
		echo "🔄 检测到文件变化，重新训练..." && \
		$(MAKE) train; \
	done 2>/dev/null || echo "⚠️  需要安装inotify-tools来启用文件监控" 